< previous page page_463 next page >

Page 463
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

         ' Wait for the shelled application to finish:
         ret& = WaitForSingleObject(proc.hProcess, INFINITE)
         ret& = CloseHandle(proc.hProcess)
      End Sub
By using the ExecCmd function from Listing 20.7, you can rewrite the example from the beginning of this section as follows:
'THIS WORKS AS INTENDED
ExecCmd "MyBat.Bat"
MsgBox "Batch file complete!"
Note that actually performing this task requires a fairly involved set of API declarations, but the ExecCmd function makes them easy to use.
Callbacks and Subclassing
API calls allow you to interact with the Windows operating system at a lower level than with standard VB code alone. When you write programs in standard VB, you are essentially using VB code as a wrapper for the Windows API. Although most of your programs will not be required to use complex API calls, at times you must use the API if the task you are trying to accomplish has not been implemented in the VB language directly. One area in which you may need the API is in processing Windows messages, which can be handled by callbacks and subclassing. These techniques are discussed in the following sections.
Callbacks
A callback is a function in your Visual Basic program that is called by Windows. Consider, for example, the declaration for the EnumWindows API function:
Declare Function EnumWindows lib "user32" (ByVal lpEnumFunc as Long, _
                                           lParam as Any) As Long
Notice that the first parameter of EnumWindows is a pointer to a function. The function pointed to is one you write in a Visual Basic moduleyour callback function. The EnumWindows function iterates through the Windows task list, executing your callback function each time. To pass a pointer to EnumWindows, you use the AddressOf keyword and the name of your callback function, as in the following example:
lRetVal = EnumWindows(AddressOf MyCallBackFunc, 12345&)
Executing the preceding line of code (only a single time) causes EnumWindows to call MyCallBackFunc repeatedly for each window in the task list. When EnumWindows calls MyCallBackFunc, it passes two parameters: a window handle and the second parameter originally passed to EnumWindows. (The second parameter can be used by your callback function however you see fit, much like the SendMessage parameters.) For this reason, callback functions are written specifically for the API that calls them. In other words, a valid callback function for EnumWindows needs to be declared according to certain rules. Specifically, it must have two parameters and return a Long value. The return value is used to stop EnumWindows before it finishes enumerating all the windows.

 
< previous page page_463 next page >